<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 过滤器的作用：文本格式化 -->

      <!-- 过滤器可以串联 -->
      <!-- 过滤器可以用在v-bind上的 -->
      <h2 :title="timer | formatTimer">过滤器</h2>
      <p>性别: {{ sex | fn | fn2 }}</p>
      <p>当前时间： {{ timer | formatTimer }}</p>
    </div>

    <script src="../Vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          sex: 1,
          timer: 1655172883419,
          list: [
            {
              id: 1,
              timer: 1655172883419,
            },
            {
              id: 2,
              timer: 1655172883420,
            },
          ],
        },
        filters: {
          fn(val) {
            return val === 1 ? "男" : "女";
          },
          fn2(val) {
            return val === "男" ? "♂" : "♀";
          },
          formatTimer(val) {
            var date = new Date(val);
            Y = date.getFullYear() + "-";
            M =
              (date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1) + "-";
            D = date.getDate() + " ";
            h = date.getHours() + ":";
            m = date.getMinutes() + ":";
            s = date.getSeconds();
            return Y + M + D + h + m + s;
          },
        },
      });
    </script>
  </body>
</html>
